package com.atguigu.daijia.rules.helper;

import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.Message;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

public class DroolsHelper {
    public static KieSession loadForRule(String drlStr){
        KieServices kieServices=KieServices.Factory.get();
        KieFileSystem kieFileSystem=kieServices.newKieFileSystem();
        kieFileSystem.write("src/main/resources/rules"+drlStr.hashCode()+".drl",drlStr);
//        将kieFileSystem加入到kieBuilder中
        KieBuilder kieBuilder=kieServices.newKieBuilder(kieFileSystem);
//        编译此时的builder中的规则
        kieBuilder.buildAll();
        if (kieBuilder.getResults().hasMessages(Message.Level.ERROR)){
            throw new RuntimeException("Build Errors:\n"+kieBuilder.getResults().toString());
        }
        KieContainer kieContainer=kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
        return kieContainer.newKieSession();
    }
}
